package com.stylefeng.guns.modular.system.task;

import java.net.URI;
import java.net.URL;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.common.constant.state.HsgtType;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.HsgtStatsMapper;
import com.stylefeng.guns.modular.system.model.HsgtStaicData;
import com.stylefeng.guns.modular.system.model.HsgtStats;

@Service
public class HsgtStatsImportTask {

	private Logger log = LoggerFactory.getLogger(HsgtStatsImportTask.class);
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	@Autowired
	private HsgtStatsMapper hsgtStatsMapper;
	
	public void execute(boolean isHoliday,HsgtType type) {
		if(isHoliday){
				String url = ImportContants.getHsgtStatsPathStr(1, type.getType());
				HttpGet httpGet = HttpClientUtil.getGetRequest();
				try {
					URL http = new URL(url);
					URI uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
					httpGet.setURI(uri);
					String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
					JSONObject jsonObj = JSON.parseObject(json);
					int pages = jsonObj.getIntValue("pages");
					parseJgzyData(jsonObj.getString("data"),type);
					for(int i= 2; i <= pages ; i++){
						url = ImportContants.getHsgtStatsPathStr(i, type.getType());
						http = new URL(url);
						uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
						httpGet.setURI(uri);
						json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
						jsonObj = JSON.parseObject(json);
						parseJgzyData(jsonObj.getString("data"),type);
					}
				} catch (Exception e) {
					log.error("get hsgt stats data is error!" + e);
				}finally{
					httpGet.abort();
				}
		}
	}
	
	private void parseJgzyData(String json,HsgtType type){
		if(StringUtils.isNotEmpty(json)){
			List<HsgtStaicData> channelItemList = JSON.parseArray(json,HsgtStaicData.class);
			for(HsgtStaicData hsgtData : channelItemList){
				HsgtStats hsgtStats = new HsgtStats();
				hsgtStats.setgSalesCode(type.getCode());
				hsgtStats.setgDetailDate(hsgtData.getDetailDate());
				
				hsgtStats = hsgtStatsMapper.selectByDate(hsgtStats);
				if(hsgtStats == null){
					hsgtStats = new HsgtStats();
					hsgtStats.setgSalesCode(type.getCode());
					hsgtStats.setgDetailDate(hsgtData.getDetailDate());
					hsgtStats.setgSalesName(type.getName());
					hsgtStats.setgDaye(hsgtData.getDRYE());
					hsgtStats.setgDrcjjme(hsgtData.getDRCJJME());
					hsgtStats.setgDrzjlr(hsgtData.getDRZJLR());
					hsgtStats.setgSlzjlr(hsgtData.getLSZJLR());
					hsgtStatsMapper.insert(hsgtStats);
				}
				
			}
		}
	}
}
